package com.starbucks.opencase.service;

import com.starbucks.opencase.config.configOC;
import com.starbucks.opencase.dao.daoAdminAccount;
import com.starbucks.opencase.data.daCode;
import com.starbucks.opencase.entity.entAdminLogin;
import com.starbucks.opencase.function.funCode;
import com.starbucks.opencase.function.funCommand;
import com.starbucks.opencase.request.reqAdminCreateAccount;
import com.starbucks.opencase.request.reqAdminLogin;
import com.starbucks.opencase.request.reqSendCode;
import com.starbucks.opencase.response.resAdminLogin;
import com.starbucks.opencase.response.resSendCode;
import com.starbucks.opencase.response.resStatus;
import com.starbucks.opencase.tool.toolAdminAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class serAdminAccount implements daoAdminAccount {
    @Autowired
    private toolAdminAccount toolAdminAccount;
    @Autowired
    private funCode funCode;
    @Autowired
    private configOC configOC;
    @Autowired
    private funCommand funCommand;
    @Override
    public resSendCode sendCode(reqSendCode info) {
        resSendCode res=new resSendCode();
        res.setTimestamp(System.currentTimeMillis());
        if(!toolAdminAccount.exist(info.getPhone())){
            res.setTip("该手机号非管理员");
            return res;
        }
        if(toolAdminAccount.queryLock(info.getPhone())){
            res.setTip("该手机号已被锁定");
            return res;
        }
        res.setStatus(!funCode.create(info.getPhone()).isEmpty());
        res.setPast_due(configOC.getPast_que());
        return res;
    }

    @Override
    public resAdminLogin login(reqAdminLogin info) {
        resAdminLogin res=new resAdminLogin();
        res.setPhone(info.getPhone());
        res.setCommand(toolAdminAccount.queryCommand(info.getPhone()));
        if(info.getCommand()==null || info.getCommand().isEmpty()){//验证码登录
            if(!daCode.codes.get(info.getPhone()).equals(info.getCode())){
                res.setTip("验证码错误");
                return res;
            }
            res.setCommand(funCommand.create(info.getPhone()));
            toolAdminAccount.setCommand(info.getPhone(),res.getCommand());
        }else{//口令登录
            if(toolAdminAccount.queryLock(info.getPhone())){
                res.setTip("该手机号已被锁定");
                return res;
            }
            String[] commands= funCommand.decode(info.getCommand());
            if(commands==null){
                res.setTip("自动登录失败0");
                return res;
            }
            if(Long.parseLong(commands[1])<res.getTimestamp()){
                res.setTip("自动登录失败1");
                return res;
            }
            if(!info.getCommand().equals(res.getCommand())){
                res.setTip("自动登录失败2");
                return res;
            }
        }
        entAdminLogin qw=toolAdminAccount.queryInfo(info.getPhone());
        if(qw==null){
            res.setTip("登录失败，请重新登录");
            return res;
        }
        res.setName(qw.getName());
        funCode.delete(info.getPhone());
        res.setTip("登录成功");
        res.setStatus(true);
        return res;
    }

    @Override
    public resStatus create(reqAdminCreateAccount info) {
        resStatus res=new resStatus();
        if(!funCode.testCode(info.getPhone(),info.getCode())){
            res.setTip("验证码错误");
            return res;
        }
        entAdminLogin ent=new entAdminLogin();
        ent.setPhone(info.getPhone());
        ent.setLock(false);
        ent.setName(info.getName());
        ent.setAuthority("""
                {
                    "forbidden":[
                        "createAdmin",
                        "editAdmin",
                        "queryAdmin"
                    ]
                }
                """);
        ent.setCommand("");
        ent.setTimestamp(0);
        res.setStatus(toolAdminAccount.add(ent));
        res.setTip(res.isStatus()?"创建成功":"创建失败");
        return res;
    }
}
